﻿var index = 0;
function onGridDataBinding() {
    var grid = $('#Grid').data('tGrid')
    index = (grid.currentPage - 1) * grid.pageSize;
}

function expandFirstRow(grid, row) {
    if (grid.$rows().index(row) == 0) {
        grid.expandRow(row);
    }
}

function master_onRowDataBound(e) {
    var grid = $(this).data('tGrid');
    expandFirstRow(grid, e.row);
}

function DeleteSelectedRecords(controllerName) {
    var deletecheckboxes = $("#Grid input[name='delete']:checked");
    var count = deletecheckboxes.length;
    var counter = 0;
    var refreshPage = false;
    var failedRows = new Array();
    var faildCounter = 0;
    deletecheckboxes.each(function () {
//        var rownumber = $(this).attr("rownumber");
        $.post('/' + controllerName + '/Delete/' + $(this).val(),
               null,
               function (json) {
                   if (json.success) {
                       refreshPage = true;
                   } else {
                       failedRows[faildCounter] = json.itemName;
                       faildCounter++;
                   }
                   counter++;
                   if (counter == count) {
                       if (failedRows.length > 0) {
                           failedRows.sort();
                           alert("Delete [" + failedRows.join(", ") + "] failed!\n");
                           //                           var errors = "";
                           //                           $.each(failedRows, function (i) {
                           //                               errors += "#" + i + ": Delete failed!\n";
                           //                           });
                           //                           alert(errors);
                       }
                       if (refreshPage == true) {
                           window.location.href = '/' + controllerName + '/';
                       }
                   }
               },
               'json');
    });
}

function SelectAllRecords() {
    if ($('#selectall').is(':checked')) {
        $("#Grid input[name='delete']").attr("checked", "true");
    }
    else
    { $("#Grid input[name='delete']").removeAttr("checked"); }
}

function SetEstimatedLifeTime(estimatedLifeTimeId, percentageId) {
    var valueText = $.trim($("#" + percentageId).val());
    var value = 0;
    if (valueText.length > 0) {
        value = Number(valueText);
    }
    $("#" + estimatedLifeTimeId).val('');
    $("#" + estimatedLifeTimeId + "Years").val('');
    $("#" + estimatedLifeTimeId + "Months").val('');
    if (value != 0) {
        var amt = 100 / value;
        var mns = Math.round(((100 % value) / value) * 12);
        var yrs = Math.floor(amt);

        $("#" + estimatedLifeTimeId).val((yrs * 12) + mns);

        if (yrs != 0) {
            $("#" + estimatedLifeTimeId + "Years").val(yrs);
        }
        if (mns != 0) {
            $("#" + estimatedLifeTimeId + "Months").val(mns);
        }
    }
}

function SetPercentage(estimatedLifeTimeId, percentageId) {
    var yearsText =$.trim($("#" + estimatedLifeTimeId + "Years").val());
    var monthsText = $.trim($("#" + estimatedLifeTimeId + "Months").val());

    var years = 0;
    var months = 0;
    if (yearsText.length > 0) {
        years = Number(yearsText);
    }
    if (monthsText.length > 0) {
        months = Number(monthsText);
    }

    $("#" + percentageId).val('');
    $("#" + estimatedLifeTimeId).val('');

    if (years != 0 || months != 0) {
        var value = 0;
        if (years != "" && months != "") {
            value = years + (months / 12);
        }
        else if (years != "") {
        value = years;
        }
    else {
        value = months / 12;
    }
        var amt = parseFloat(100 / value);
        $("#" + percentageId).val(amt.toFixed(2));
        $("#" + estimatedLifeTimeId).val((years * 12) + months);
    }
}

//function SetEstimatedLifeTime(e, estimatedLifeTimeId) {
//    var value = e.value;
//    $("#" + estimatedLifeTimeId).val('');
//    if (value != "") {
//        $("#" + estimatedLifeTimeId).val(100 / value);
//    }
//}

//function SetPercentage(e, percentageId) {
//    var value = e.value;
//    $("#" + percentageId).val('');
//    if (value != "") {
//        var amt = parseFloat(100 / value);
//        $("#" + percentageId).val(amt.toFixed(2));
//    }
//}

function GetRelationDetails(entityName, parameterName, parameterValue , language) {
    var holderName='#Details_'+entityName+'_'+parameterValue;
    var linkName='#DetailsLink_'+entityName+'_'+parameterValue;
    var loadingImgName = '#DetailsLoading_' + entityName + '_' + parameterValue;
   
                    $(linkName).hide();
                    $(loadingImgName).show();
                     
                     $(holderName).html('');
                     $(holderName).load('/'+language+'/Home/GetRelationDetails/' + entityName + '$' + parameterName + '$' + parameterValue, function (html) {
                         $(holderName)[0].value = html;
                         $(loadingImgName).hide();
                     });
}


$(document).ready(function () {
    //Setup the ajax indicator
    //TODO We'll not be able to load ajax-loading.gif when site is run in virtual directory. Fix it 
    //1. Move to .cshtml file and use @Url.Content there)
    //2. Or create css style

    $('body').append('<div id="ajaxBusy" style="display:none;background-color: #000000;height: 100%;left: 0;opacity: 0.5;position: fixed;top: 0;width: 100%;z-index: 10000;"><table style="width: 100%; height: 100%"><tr><td style="text-align: center; vertical-align: middle;"><img src="/content/media/images/ajax-loading.gif"></td></tr></table></div>');
//    $('#ajaxBusy').css({
//        display: "none",
//        margin: "0px",
//        paddingLeft: "0px",
//        paddingRight: "0px",
//        paddingTop: "0px",
//        paddingBottom: "0px",
//        position: "absolute",
//        right: "3px",
//        top: "3px",
//        width: "100%",
//        height:"100%",
//        position: "fixed"
//    });
});
// Ajax activity indicator bound to ajax start/stop document events
$(document).ajaxStart(function () {
    $('#ajaxBusy').show();
}).ajaxStop(function () {
    $('#ajaxBusy').hide();
});

$.fn.exists = function () {
    return this.length !== 0;
}

